<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/3
 * Time: 14:10
 */

namespace app\sports_admin\model;


use think\Model;
use think\db;
use traits\model\SoftDelete;

class Teacher extends Model
{
    use SoftDelete;
    protected $table = "teacher";
    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['pwd','update_time','create_time'];

    public function school()
    {
        return $this->belongsTo('school','school_id','school_id')->field('school_id,school_name');
    }

    /**
     * 获取教师信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
    */
    public static function getTeacherInfo($page,$rows,$sort,$sortOrder,$searchName,$schoolId)
    {
        $result = self::with('school')->where(function($query)use($searchName){
            if($searchName){
                $query->where('teacher_name|teacher_phone','like',"%{$searchName}%");
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)
        ->where('school_id',$schoolId)->select();

        $count = self::with('school')->where(function($query)use($searchName){
            if($searchName){
                $query->where('teacher_name|teacher_phone','like',"%{$searchName}%");
            }
        })->where('school_id',$schoolId)->count();
        return json(['total'=>$count,'rows'=>$result]);
    }
}